﻿using System;
using System.Web.Mvc;
using MedicalLog.Domain.Entities;
using MedicalLog.Domain.Repositories;
using System.Globalization;

namespace MedicalLog.WebUI.Controllers
{
    public class TutorController : Controller
    {
        TutorRepository tutorRepo;
        PatientsRepository patientRepository;
        Tutor tutor;
        Patient patient;
        Insurance insurance;

        public TutorController()
        {
            tutorRepo = new TutorRepository();
            patientRepository = new PatientsRepository();
            insurance = new Insurance();
        }

        #region Controllers
        //
        // GET: /Tutor/

        public ActionResult Create()
        {
            return View();
        }


        [HttpPost]
        public ActionResult Create(FormCollection tutorPatientForm)
        {
            if (ModelState.IsValid)
            {
                patientRepository.AddPatient(new PatientController().RemoveStringFormaters(GetPatient(tutorPatientForm)));
                tutorRepo.AddTutor(RemoveStringFormaters(GetTutor(tutorPatientForm)), patient);

                return RedirectToAction("searchPatient", "Patient");
            }
            else
            {
                return PartialView(tutor);
            }
        }


        #endregion


        #region Utility Methods

        private Patient GetPatient(FormCollection tutorPatientForm)
        {
            
            patient = new Patient();
            patient.Insurance = new Insurance();
            patient.Name = tutorPatientForm[7];
            patient.LastName = tutorPatientForm[8];
            patient.Telephone = tutorPatientForm[9];
            patient.Celphone = tutorPatientForm[10];
            patient.Address = tutorPatientForm[11];
            patient.DateofBirth = DateTime.ParseExact(tutorPatientForm[12], "dd/MM/yyyy", CultureInfo.InvariantCulture);

            patient.IdentificationNumber = tutorPatientForm[13];
            patient.Gender = (tutorPatientForm[14] == "True") ? true : false;
            patient.Passport = tutorPatientForm[15];
            patient.IdInsurance = Convert.ToInt16(tutorPatientForm[16]);
            patient.Active = (tutorPatientForm[17] == "True") ? true : false;

            
            return patient;
        }


        private Tutor GetTutor(FormCollection tutorPatientForm)
        {
            tutor = new Tutor();
            tutor.Name = tutorPatientForm[0];
            tutor.LastName = tutorPatientForm[1];
            tutor.Telephone = tutorPatientForm[2];
            tutor.Celphone = tutorPatientForm[3];
            tutor.Address = tutorPatientForm[4];
            tutor.IdentificationNumber = tutorPatientForm[5];
            tutor.Passport = tutorPatientForm[6];

            return tutor;
        }
        private Tutor RemoveStringFormaters(Tutor tutor)
        {

            tutor.Telephone = tutor.Telephone.Replace("(", "");
            tutor.Telephone = tutor.Telephone.Replace(")", "");
            tutor.Telephone = tutor.Telephone.Replace("-", "");
            tutor.Telephone = tutor.Telephone.Replace(" ", "");

            tutor.Celphone = tutor.Celphone.Replace("(", "");
            tutor.Celphone = tutor.Celphone.Replace(")", "");
            tutor.Celphone = tutor.Celphone.Replace("-", "");
            tutor.Celphone = tutor.Celphone.Replace(" ", "");

            tutor.IdentificationNumber = tutor.IdentificationNumber.Replace("-", "");
            return tutor;
        }
        #endregion
    }
}
